﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;

namespace BAL
{
    public class School : BaseBAL<SchoolInfo, Models.School>
    {
        private static string CacheName="AllSchool";
        /// <summary>
        /// 取得一条
        /// </summary>
        /// <param name="InputId">信息ID</param>
        /// <returns></returns>
        public static SchoolInfo GetSingleInfo(int InputId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.School> schoolInfo = (IList<Models.School>)CacheHelper.GetCache(CacheName);
            return TransT2ToT1(schoolInfo.Single(b => b.Id == InputId));
        }
        /// <summary>
        /// 通过名称查询对象
        /// </summary>
        /// <returns></returns>
        public static SchoolInfo GetSingleInfoBySchoolName(string SchoolName) 
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.School> schoolInfo = (IList<Models.School>)CacheHelper.GetCache(CacheName);
            return TransT2ToT1(schoolInfo.Single(b => b.Name== SchoolName));
        }
        public static SchoolInfo GetSingleInfoByKeyword(string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.School> schoolInfo = (IList<Models.School>)CacheHelper.GetCache(CacheName);
            return TransT2ToT1(schoolInfo.Single(b => b.Keyword == keyword));
        }
        public static IList<SchoolInfo> GetSchoolInfoByAreaId(int areaId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.School> schoolInfo = (IList<Models.School>)CacheHelper.GetCache(CacheName);
            return schoolInfo.Where(b => b.AreaId == areaId).Select(b => TransT2ToT1(b)).ToList();
        }

        public static IList<int> GetSchoolIdByAreaId(int areaId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.School> schoolInfo = (IList<Models.School>)CacheHelper.GetCache(CacheName);
            return schoolInfo.Where(b => b.AreaId == areaId).Select(b => b.Id).ToList();
        }

        /// <summary>
        /// 取得全部列表
        /// </summary>
        /// <returns></returns>
        public static IList<SchoolInfo> GetList()
        {
            if (!CacheHelper.IsExist(CacheName))
            {
                ToCache();
            }
            IList<Models.School> schoolInfo = (IList<Models.School>)CacheHelper.GetCache(CacheName);
            return schoolInfo.Select(t => TransT2ToT1(t)).ToList();
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static SchoolInfo Save(SchoolInfo InstInfo)
        {
            SchoolInfo schoolInfo = InstInfo;
            if (InstInfo.Id > 0)
            {
                Edit(InstInfo);
            }
            else
            {
                schoolInfo= Add(InstInfo);
            }
            ToCache();
            return schoolInfo;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">id</param>
        public static void DelInfo(int id)
        {
            Users.DelBySchoolId(id);
            Grade.DelBySchoolId(id);
            Delete(id);
            ToCache();
        }
        public static void DelInfo(IList<int> ids )
        {
            foreach (int id in ids)
            {
                DelInfo(id);
            }
        }
        public static void DeleteByAreaId(int id)
        {
            IList<int> ids = GetSchoolIdByAreaId(id);
            DelInfo(ids);
        }
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.Schools.ToList(), 0, 60);
            }
        }
    }
}
